home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxshade6.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  19.4 KB  |  617 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxshade6.c,v 1.3 2000/09/19 19:00:40 lpd Exp $ */
  20. /* Rendering for Coons and tensor patch shadings */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsmatrix.h"        /* for gscoord.h */
  25. #include "gscoord.h"
  26. #include "gxcspace.h"
  27. #include "gxdcolor.h"
  28. #include "gxistate.h"
  29. #include "gxshade.h"
  30. #include "gxshade4.h"
  31. #include "gzpath.h"
  32.  
  33. /* ================ Utilities ================ */
  34.  
  35. /* Define one segment (vertex and next control points) of a curve. */
  36. typedef struct patch_curve_s {
  37.     mesh_vertex_t vertex;
  38.     gs_fixed_point control[2];
  39. } patch_curve_t;
  40.  
  41. /* Get colors for patch vertices. */
  42. private int
  43. shade_next_colors(shade_coord_stream_t * cs, patch_curve_t * curves,
  44.           int num_vertices)
  45. {
  46.     int i, code = 0;
  47.  
  48.     for (i = 0; i < num_vertices && code >= 0; ++i)
  49.     code = shade_next_color(cs, curves[i].vertex.cc);
  50.     return code;
  51. }
  52.  
  53. /* Get a Bezier or tensor patch element. */
  54. private int
  55. shade_next_curve(shade_coord_stream_t * cs, patch_curve_t * curve)
  56. {
  57.     int code = shade_next_coords(cs, &curve->vertex.p, 1);
  58.  
  59.     if (code >= 0)
  60.     code = shade_next_coords(cs, curve->control,
  61.                  countof(curve->control));
  62.     return code;
  63. }
  64.  
  65. /* Define a color to be used in curve rendering. */
  66. /* This may be a real client color, or a parametric function argument. */
  67. typedef struct patch_color_s {
  68.     float t;            /* parametric value */
  69.     gs_client_color cc;
  70. } patch_color_t;
  71.  
  72. /*
  73.  * Parse the next patch out of the input stream.  Return 1 if done,
  74.  * 0 if patch, <0 on error.
  75.  */
  76. private int
  77. shade_next_patch(shade_coord_stream_t * cs, int BitsPerFlag,
  78. patch_curve_t curve[4], gs_fixed_point interior[4] /* 0 for Coons patch */ )
  79. {
  80.     int flag = shade_next_flag(cs, BitsPerFlag);
  81.     int num_colors, code;
  82.  
  83.     if (flag < 0)
  84.     return 1;        /* no more data */
  85.     switch (flag & 3) {
  86.     default:
  87.         return_error(gs_error_rangecheck);    /* not possible */
  88.     case 0:
  89.         if ((code = shade_next_curve(cs, &curve[0])) < 0 ||
  90.         (code = shade_next_coords(cs, &curve[1].vertex.p, 1)) < 0
  91.         )
  92.         return code;
  93.         num_colors = 4;
  94.         goto vx;
  95.     case 1:
  96.         curve[0] = curve[1], curve[1].vertex = curve[2].vertex;
  97.         goto v3;
  98.     case 2:
  99.         curve[0] = curve[2], curve[1].vertex = curve[3].vertex;
  100.         goto v3;
  101.     case 3:
  102.         curve[1].vertex = curve[0].vertex, curve[0] = curve[3];
  103. v3:        num_colors = 2;
  104. vx:        if ((code = shade_next_coords(cs, curve[1].control, 2)) < 0 ||
  105.         (code = shade_next_curve(cs, &curve[2])) < 0 ||
  106.         (code = shade_next_curve(cs, &curve[3])) < 0 ||
  107.         (interior != 0 &&
  108.          (code = shade_next_coords(cs, interior, 4)) < 0) ||
  109.         (code = shade_next_colors(cs, &curve[4 - num_colors],
  110.                       num_colors)) < 0
  111.         )
  112.         return code;
  113.     }
  114.     return 0;
  115. }
  116.  
  117. /* Define the common state for rendering Coons and tensor patches. */
  118. typedef struct patch_fill_state_s {
  119.     mesh_fill_state_common;
  120.     const gs_function_t *Function;
  121. } patch_fill_state_t;
  122.  
  123. /*
  124.  * Calculate the interpolated color at a given point.
  125.  * Note that we must do this twice for bilinear interpolation.
  126.  * We use the name ppcr rather than ppc because an Apple compiler defines
  127.  * ppc when compiling on PowerPC systems (!).
  128.  */
  129. private void
  130. patch_interpolate_color(patch_color_t * ppcr, const patch_color_t * ppc0,
  131.        const patch_color_t * ppc1, const patch_fill_state_t * pfs, floatp t)
  132. {
  133.     if (pfs->Function)
  134.     ppcr->t = ppc0->t + t * (ppc1->t - ppc0->t);
  135.     else {
  136.     int ci;
  137.  
  138.     for (ci = pfs->num_components - 1; ci >= 0; --ci)
  139.         ppcr->cc.paint.values[ci] =
  140.         ppc0->cc.paint.values[ci] +
  141.         t * (ppc1->cc.paint.values[ci] - ppc0->cc.paint.values[ci]);
  142.     }
  143. }
  144.  
  145. /* Resolve a patch color using the Function if necessary. */
  146. private void
  147. patch_resolve_color(patch_color_t * ppcr, const patch_fill_state_t * pfs)
  148. {
  149.     if (pfs->Function)
  150.     gs_function_evaluate(pfs->Function, &ppcr->t, ppcr->cc.paint.values);
  151. }
  152.  
  153. /* ================ Specific shadings ================ */
  154.  
  155. /*
  156.  * The curves are stored in a clockwise or counter-clockwise order that maps
  157.  * to the patch definition in a non-intuitive way.  The documentation
  158.  * (pp. 277-281 of the PostScript Language Reference Manual, Third Edition)
  159.  * says that the curves are in the order D1, C2, D2, C1.
  160.  */
  161. /* The starting points of the curves: */
  162. #define D1START 0
  163. #define C2START 1
  164. #define D2START 3
  165. #define C1START 0
  166. /* The control points of the curves (x means reversed order): */
  167. #define D1CTRL 0
  168. #define C2CTRL 1
  169. #define D2XCTRL 2
  170. #define C1XCTRL 3
  171. /* The end points of the curves: */
  172. #define D1END 1
  173. #define C2END 2
  174. #define D2END 2
  175. #define C1END 3
  176.  
  177. /* ---------------- Common code ---------------- */
  178.  
  179. /* Evaluate a curve at a given point. */
  180. private void
  181. curve_eval(gs_fixed_point * pt, const gs_fixed_point * p0,
  182.        const gs_fixed_point * p1, const gs_fixed_point * p2,
  183.        const gs_fixed_point * p3, floatp t)
  184. {
  185.     fixed a, b, c, d;
  186.     fixed t01, t12;
  187.  
  188.     d = p0->x;
  189.     curve_points_to_coefficients(d, p1->x, p2->x, p3->x,
  190.                  a, b, c, t01, t12);
  191.     pt->x = (fixed) (((a * t + b) * t + c) * t + d);
  192.     d = p0->y;
  193.     curve_points_to_coefficients(d, p1->y, p2->y, p3->y,
  194.                  a, b, c, t01, t12);
  195.     pt->y = (fixed) (((a * t + b) * t + c) * t + d);
  196.     if_debug3('2', "[2]t=%g => (%g,%g)\n", t, fixed2float(pt->x),
  197.           fixed2float(pt->y));
  198. }
  199.  
  200. /*
  201.  * Merge two arrays of splits, sorted in increasing order.
  202.  * Return the number of entries in the result, which might be less than
  203.  * n1 + n2 (if an a1 entry is equal to an a2 entry).
  204.  * a1 or a2 may overlap out as long as a1 - out >= n2 or a2 - out >= n1
  205.  * respectively.
  206.  */
  207. private int
  208. merge_splits(double *out, const double *a1, int n1, const double *a2, int n2)
  209. {
  210.     double *p = out;
  211.     int i1 = 0, i2 = 0;
  212.  
  213.     /*
  214.      * We would like to write the body of the loop as an assignement
  215.      * with a conditional expression on the right, but gcc 2.7.2.3
  216.      * generates incorrect code if we do this.
  217.      */
  218.     while (i1 < n1 || i2 < n2)
  219.     if (i1 == n1)
  220.         *p++ = a2[i2++];
  221.     else if (i2 == n2 || a1[i1] < a2[i2])
  222.         *p++ = a1[i1++];
  223.     else if (a1[i1] > a2[i2])
  224.         *p++ = a2[i2++];
  225.     else
  226.         i1++, *p++ = a2[i2++];
  227.     return p - out;
  228. }
  229.  
  230. /*
  231.  * Split a curve in both X and Y.  Return the number of split points.
  232.  * swap = 0 if the control points are in order, 1 if reversed.
  233.  */
  234. private int
  235. split_xy(double out[4], const gs_fixed_point *p0, const gs_fixed_point *p1,
  236.      const gs_fixed_point *p2, const gs_fixed_point *p3)
  237. {
  238.     double tx[2], ty[2];
  239.  
  240.     return merge_splits(out, tx,
  241.             gx_curve_monotonic_points(p0->x, p1->x, p2->x, p3->x,
  242.                           tx),
  243.             ty,
  244.             gx_curve_monotonic_points(p0->y, p1->y, p2->y, p3->y,
  245.                           ty));
  246. }
  247.  
  248. /*
  249.  * Compute the joint split points of 2 curves.
  250.  * swap = 0 if the control points are in order, 1 if reversed.
  251.  * Return the number of split points.
  252.  */
  253. inline private int
  254. split2_xy(double out[8], const gs_fixed_point *p10, const gs_fixed_point *p11,
  255.       const gs_fixed_point *p12, const gs_fixed_point *p13,
  256.       const gs_fixed_point *p20, const gs_fixed_point *p21,
  257.       const gs_fixed_point *p22, const gs_fixed_point *p23)
  258. {
  259.     double t1[4], t2[4];
  260.  
  261.     return merge_splits(out, t1, split_xy(t1, p10, p11, p12, p13),
  262.             t2, split_xy(t2, p20, p21, p22, p23));
  263. }
  264.  
  265. private int
  266. patch_fill(patch_fill_state_t * pfs, const patch_curve_t curve[4],
  267.        const gs_fixed_point interior[4],
  268.        void (*transform) (P5(gs_fixed_point *, const patch_curve_t[4],
  269.                  const gs_fixed_point[4], floatp, floatp)))
  270. {    /*
  271.      * The specification says the output must appear to be produced in
  272.      * order of increasing values of v, and for equal v, in order of
  273.      * increasing u.  However, all we actually have to do is follow this
  274.      * order with respect to sub-patches that might overlap, which can
  275.      * only occur as a result of non-monotonic curves; we can render
  276.      * each monotonic sub-patch in any order we want.  Therefore, we
  277.      * begin by breaking up the patch into pieces that are monotonic
  278.      * with respect to all 4 edges.  Since each edge has no more than
  279.      * 2 X and 2 Y split points (for a total of 4), taking both edges
  280.      * together we have a maximum of 8 split points for each axis.
  281.      */
  282.     double su[9], sv[9];
  283.     int nu = split2_xy(su, &curve[C1START].vertex.p,&curve[C1XCTRL].control[1],
  284.                &curve[C1XCTRL].control[0], &curve[C1END].vertex.p,
  285.                &curve[C2START].vertex.p, &curve[C2CTRL].control[0],
  286.                &curve[C2CTRL].control[1], &curve[C2END].vertex.p);
  287.     int nv = split2_xy(sv, &curve[D1START].vertex.p, &curve[D1CTRL].control[0],
  288.                &curve[D1CTRL].control[1], &curve[D1END].vertex.p,
  289.                &curve[D2START].vertex.p, &curve[D2XCTRL].control[1],
  290.                &curve[D2XCTRL].control[0], &curve[D2END].vertex.p);
  291.     int iu, iv, ju, jv, ku, kv;
  292.     double du, dv;
  293.     double v0, v1, vn, u0, u1, un;
  294.     patch_color_t c00, c01, c10, c11;
  295.     /*
  296.      * At some future time, we should set check = false if the curves
  297.      * fall entirely within the bounding rectangle.  (Only a small
  298.      * performance optimization, to avoid making this check for each
  299.      * triangle.)
  300.      */
  301.     bool check = true;
  302.  
  303. #ifdef DEBUG
  304.     if (gs_debug_c('2')) {
  305.     int k;
  306.  
  307.     dlputs("[2]patch curves:\n");
  308.     for (k = 0; k < 4; ++k)
  309.         dprintf6("        (%g,%g) (%g,%g)(%g,%g)\n",
  310.              fixed2float(curve[k].vertex.p.x),
  311.              fixed2float(curve[k].vertex.p.y),
  312.              fixed2float(curve[k].control[0].x),
  313.              fixed2float(curve[k].control[0].y),
  314.              fixed2float(curve[k].control[1].x),
  315.              fixed2float(curve[k].control[1].y));
  316.     if (nu > 1) {
  317.         dlputs("[2]Splitting u");
  318.         for (k = 0; k < nu; ++k)
  319.         dprintf1(", %g", su[k]);
  320.         dputs("\n");
  321.     }
  322.     if (nv > 1) {
  323.         dlputs("[2]Splitting v");
  324.         for (k = 0; k < nv; ++k)
  325.         dprintf1(", %g", sv[k]);
  326.         dputs("\n");
  327.     }
  328.     }
  329. #endif
  330.     /* Add boundary values to simplify the iteration. */
  331.     su[nu] = 1;
  332.     sv[nv] = 1;
  333.  
  334.     /*
  335.      * We're going to fill the curves by flattening them and then filling
  336.      * the resulting triangles.  Start by computing the number of
  337.      * segments required for flattening each side of the patch.
  338.      */
  339.     {
  340.     fixed flatness = float2fixed(pfs->pis->flatness);
  341.     int i;
  342.     int log2_k[4];
  343.  
  344.     for (i = 0; i < 4; ++i) {
  345.         curve_segment cseg;
  346.  
  347.         cseg.p1 = curve[i].control[0];
  348.         cseg.p2 = curve[i].control[1];
  349.         cseg.pt = curve[(i + 1) & 3].vertex.p;
  350.         log2_k[i] =
  351.         gx_curve_log2_samples(curve[i].vertex.p.x, curve[i].vertex.p.y,
  352.                       &cseg, flatness);
  353.     }
  354.     ku = 1 << max(log2_k[1], log2_k[3]);
  355.     kv = 1 << max(log2_k[0], log2_k[2]);
  356.     }
  357.  
  358.     /* Precompute the colors at the corners. */
  359.  
  360. #define PATCH_SET_COLOR(c, v)\
  361.   if ( pfs->Function ) c.t = v.cc[0];\
  362.   else memcpy(c.cc.paint.values, v.cc, sizeof(c.cc.paint.values))
  363.  
  364.     PATCH_SET_COLOR(c00, curve[D1START].vertex); /* = C1START */
  365.     PATCH_SET_COLOR(c01, curve[D1END].vertex); /* = C2START */
  366.     PATCH_SET_COLOR(c11, curve[C2END].vertex); /* = D2END */
  367.     PATCH_SET_COLOR(c10, curve[C1END].vertex); /* = D2START */
  368.  
  369. #undef PATCH_SET_COLOR
  370.  
  371.     /*
  372.      * Since ku and kv are powers of 2, and since log2(k) is surely less
  373.      * than the number of bits in the mantissa of a double, 1/k ...
  374.      * (k-1)/k can all be represented exactly as doubles.
  375.      */
  376.     du = 1.0 / ku;
  377.     dv = 1.0 / kv;
  378.  
  379.     /* Now iterate over the sub-patches. */
  380.     for (iv = 0, jv = 0, v0 = 0, v1 = vn = dv; jv < kv; v0 = v1, v1 = vn) {
  381.     patch_color_t c0v0, c0v1, c1v0, c1v1;
  382.  
  383.     /* Subdivide the interval if it crosses a split point. */
  384.  
  385. #define CHECK_SPLIT(ix, jx, x1, xn, dx, ax)\
  386.   if (x1 > ax[ix])\
  387.       x1 = ax[ix++];\
  388.   else {\
  389.       xn += dx;\
  390.       jx++;\
  391.       if (x1 == ax[ix])\
  392.       ix++;\
  393.   }
  394.  
  395.     CHECK_SPLIT(iv, jv, v1, vn, dv, sv);
  396.  
  397.     patch_interpolate_color(&c0v0, &c00, &c01, pfs, v0);
  398.     patch_interpolate_color(&c0v1, &c00, &c01, pfs, v1);
  399.     patch_interpolate_color(&c1v0, &c10, &c11, pfs, v0);
  400.     patch_interpolate_color(&c1v1, &c10, &c11, pfs, v1);
  401.  
  402.     for (iu = 0, ju = 0, u0 = 0, u1 = un = du; ju < ku; u0 = u1, u1 = un) {
  403.         patch_color_t cu0v0, cu1v0, cu0v1, cu1v1;
  404.         int code;
  405.  
  406.         CHECK_SPLIT(iu, ju, u1, un, du, su);
  407.  
  408. #undef CHECK_SPLIT
  409.  
  410.         patch_interpolate_color(&cu0v0, &c0v0, &c1v0, pfs, u0);
  411.         patch_resolve_color(&cu0v0, pfs);
  412.         patch_interpolate_color(&cu1v0, &c0v0, &c1v0, pfs, u1);
  413.         patch_resolve_color(&cu1v0, pfs);
  414.         patch_interpolate_color(&cu0v1, &c0v1, &c1v1, pfs, u0);
  415.         patch_resolve_color(&cu0v1, pfs);
  416.         patch_interpolate_color(&cu1v1, &c0v1, &c1v1, pfs, u1);
  417.         patch_resolve_color(&cu1v1, pfs);
  418.         if_debug6('2', "[2]u[%d]=[%g .. %g], v[%d]=[%g .. %g]\n",
  419.               iu, u0, u1, iv, v0, v1);
  420.  
  421.         /* Fill the sub-patch given by ((u0,v0),(u1,v1)). */
  422.         {
  423.         mesh_vertex_t mu0v0, mu1v0, mu1v1, mu0v1;
  424.  
  425.         (*transform)(&mu0v0.p, curve, interior, u0, v0);
  426.         (*transform)(&mu1v0.p, curve, interior, u1, v0);
  427.         (*transform)(&mu1v1.p, curve, interior, u1, v1);
  428.         (*transform)(&mu0v1.p, curve, interior, u0, v1);
  429.         if_debug4('2', "[2]  => (%g,%g), (%g,%g),\n",
  430.               fixed2float(mu0v0.p.x), fixed2float(mu0v0.p.y),
  431.               fixed2float(mu1v0.p.x), fixed2float(mu1v0.p.y));
  432.         if_debug4('2', "[2]     (%g,%g), (%g,%g)\n",
  433.               fixed2float(mu1v1.p.x), fixed2float(mu1v1.p.y),
  434.               fixed2float(mu0v1.p.x), fixed2float(mu0v1.p.y));
  435.         memcpy(mu0v0.cc, cu0v0.cc.paint.values, sizeof(mu0v0.cc));
  436.         memcpy(mu1v0.cc, cu1v0.cc.paint.values, sizeof(mu1v0.cc));
  437.         memcpy(mu1v1.cc, cu1v1.cc.paint.values, sizeof(mu1v1.cc));
  438.         memcpy(mu0v1.cc, cu0v1.cc.paint.values, sizeof(mu0v1.cc));
  439. /* Make this a procedure later.... */
  440. #define FILL_TRI(pva, pvb, pvc)\
  441.   BEGIN\
  442.     mesh_init_fill_triangle((mesh_fill_state_t *)pfs, pva, pvb, pvc, check);\
  443.     code = mesh_fill_triangle((mesh_fill_state_t *)pfs);\
  444.     if (code < 0)\
  445.     return code;\
  446.   END
  447. #if 0
  448.         FILL_TRI(&mu0v0, &mu1v1, &mu1v0);
  449.         FILL_TRI(&mu0v0, &mu1v1, &mu0v1);
  450. #else
  451.         {
  452.             mesh_vertex_t mmid;
  453.             int ci;
  454.  
  455.             (*transform)(&mmid.p, curve, interior,
  456.                  (u0 + u1) * 0.5, (v0 + v1) * 0.5);
  457.             for (ci = 0; ci < pfs->num_components; ++ci)
  458.             mmid.cc[ci] =
  459.                 (mu0v0.cc[ci] + mu1v0.cc[ci] +
  460.                  mu1v1.cc[ci] + mu0v1.cc[ci]) * 0.25;
  461.             FILL_TRI(&mu0v0, &mu1v0, &mmid);
  462.             FILL_TRI(&mu1v0, &mu1v1, &mmid);
  463.             FILL_TRI(&mu1v1, &mu0v1, &mmid);
  464.             FILL_TRI(&mu0v1, &mu0v0, &mmid);
  465.         }
  466. #endif
  467.         }
  468.     }
  469.     }
  470.     return 0;
  471. }
  472.  
  473. /* ---------------- Coons patch shading ---------------- */
  474.  
  475. /* Calculate the device-space coordinate corresponding to (u,v). */
  476. private void
  477. Cp_transform(gs_fixed_point * pt, const patch_curve_t curve[4],
  478.          const gs_fixed_point ignore_interior[4], floatp u, floatp v)
  479. {
  480.     double co_u = 1.0 - u, co_v = 1.0 - v;
  481.     gs_fixed_point c1u, d1v, c2u, d2v;
  482.  
  483.     curve_eval(&c1u, &curve[C1START].vertex.p,
  484.            &curve[C1XCTRL].control[1], &curve[C1XCTRL].control[0],
  485.            &curve[C1END].vertex.p, u);
  486.     curve_eval(&d1v, &curve[D1START].vertex.p,
  487.            &curve[D1CTRL].control[0], &curve[D1CTRL].control[1],
  488.            &curve[D1END].vertex.p, v);
  489.     curve_eval(&c2u, &curve[C2START].vertex.p,
  490.            &curve[C2CTRL].control[0], &curve[C2CTRL].control[1],
  491.            &curve[C2END].vertex.p, u);
  492.     curve_eval(&d2v, &curve[D2START].vertex.p,
  493.            &curve[D2XCTRL].control[1], &curve[D2XCTRL].control[0],
  494.            &curve[D2END].vertex.p, v);
  495. #define COMPUTE_COORD(xy)\
  496.     pt->xy = (fixed)\
  497.     ((co_v * c1u.xy + v * c2u.xy) + (co_u * d1v.xy + u * d2v.xy) -\
  498.      (co_v * (co_u * curve[C1START].vertex.p.xy +\
  499.           u * curve[C1END].vertex.p.xy) +\
  500.       v * (co_u * curve[C2START].vertex.p.xy +\
  501.            u * curve[C2END].vertex.p.xy)))
  502.     COMPUTE_COORD(x);
  503.     COMPUTE_COORD(y);
  504. #undef COMPUTE_COORD
  505.     if_debug4('2', "[2](u=%g,v=%g) => (%g,%g)\n",
  506.           u, v, fixed2float(pt->x), fixed2float(pt->y));
  507. }
  508.  
  509. int
  510. gs_shading_Cp_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
  511.                  gx_device * dev, gs_imager_state * pis)
  512. {
  513.     const gs_shading_Cp_t * const psh = (const gs_shading_Cp_t *)psh0;
  514.     patch_fill_state_t state;
  515.     shade_coord_stream_t cs;
  516.     patch_curve_t curve[4];
  517.     int code;
  518.  
  519.     mesh_init_fill_state((mesh_fill_state_t *) & state,
  520.              (const gs_shading_mesh_t *)psh0, rect, dev, pis);
  521.     state.Function = psh->params.Function;
  522.     shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
  523.             pis);
  524.     while ((code = shade_next_patch(&cs, psh->params.BitsPerFlag,
  525.                     curve, NULL)) == 0 &&
  526.        (code = patch_fill(&state, curve, NULL, Cp_transform)) >= 0
  527.     )
  528.     DO_NOTHING;
  529.     return min(code, 0);
  530. }
  531.  
  532. /* ---------------- Tensor product patch shading ---------------- */
  533.  
  534. /* Calculate the device-space coordinate corresponding to (u,v). */
  535. private void
  536. Tpp_transform(gs_fixed_point * pt, const patch_curve_t curve[4],
  537.           const gs_fixed_point interior[4], floatp u, floatp v)
  538. {
  539.     double Bu[4], Bv[4];
  540.     gs_fixed_point pts[4][4];
  541.     int i, j;
  542.     double x = 0, y = 0;
  543.  
  544.     /* Compute the Bernstein polynomials of u and v. */
  545.     {
  546.     double u2 = u * u, co_u = 1.0 - u, co_u2 = co_u * co_u;
  547.     double v2 = v * v, co_v = 1.0 - v, co_v2 = co_v * co_v;
  548.  
  549.     Bu[0] = co_u * co_u2, Bu[1] = 3 * u * co_u2,
  550.         Bu[2] = 3 * u2 * co_u, Bu[3] = u * u2;
  551.     Bv[0] = co_v * co_v2, Bv[1] = 3 * v * co_v2,
  552.         Bv[2] = 3 * v2 * co_v, Bv[3] = v * v2;
  553.     }
  554.  
  555.     /* Arrange the points into an indexable order. */
  556.     pts[0][0] = curve[0].vertex.p;
  557.     pts[0][1] = curve[0].control[0];
  558.     pts[0][2] = curve[0].control[1];
  559.     pts[0][3] = curve[1].vertex.p;
  560.     pts[1][3] = curve[1].control[0];
  561.     pts[2][3] = curve[1].control[1];
  562.     pts[3][3] = curve[2].vertex.p;
  563.     pts[3][2] = curve[2].control[0];
  564.     pts[3][1] = curve[2].control[1];
  565.     pts[3][0] = curve[3].vertex.p;
  566.     pts[2][0] = curve[3].control[0];
  567.     pts[1][0] = curve[3].control[1];
  568.     pts[1][1] = interior[0];
  569.     pts[2][1] = interior[1];
  570.     pts[2][2] = interior[2];
  571.     pts[1][2] = interior[3];
  572.  
  573.     /* Now compute the actual point. */
  574.     for (i = 0; i < 4; ++i)
  575.     for (j = 0; j < 4; ++j) {
  576.         double coeff = Bu[i] * Bv[j];
  577.  
  578.         x += pts[i][j].x * coeff, y += pts[i][j].y * coeff;
  579.     }
  580.     pt->x = (fixed)x, pt->y = (fixed)y;
  581. }
  582.  
  583. int
  584. gs_shading_Tpp_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
  585.                   gx_device * dev, gs_imager_state * pis)
  586. {
  587.     const gs_shading_Tpp_t * const psh = (const gs_shading_Tpp_t *)psh0;
  588.     patch_fill_state_t state;
  589.     shade_coord_stream_t cs;
  590.     patch_curve_t curve[4];
  591.     gs_fixed_point interior[4];
  592.     int code;
  593.  
  594.     mesh_init_fill_state((mesh_fill_state_t *) & state,
  595.              (const gs_shading_mesh_t *)psh0, rect, dev, pis);
  596.     state.Function = psh->params.Function;
  597.     shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
  598.             pis);
  599.     while ((code = shade_next_patch(&cs, psh->params.BitsPerFlag,
  600.                     curve, interior)) == 0) {
  601.     /*
  602.      * The order of points appears to be consistent with that for Coons
  603.      * patches, which is different from that documented in Red Book 3.
  604.      */
  605.     gs_fixed_point swapped_interior[4];
  606.  
  607.     swapped_interior[0] = interior[0];
  608.     swapped_interior[1] = interior[3];
  609.     swapped_interior[2] = interior[2];
  610.     swapped_interior[3] = interior[1];
  611.     code = patch_fill(&state, curve, swapped_interior, Tpp_transform);
  612.     if (code < 0)
  613.         break;
  614.     }
  615.     return min(code, 0);
  616. }
  617.